package com.casic.worklog.controller;

import com.casic.worklog.model.Employee;
import com.casic.worklog.model.vo.Result;
import com.casic.worklog.service.EmployeeService;
import com.casic.worklog.util.BsResult;
import com.casic.worklog.util.CommonUtil;
import com.casic.worklog.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import static com.casic.worklog.model.vo.Result.error;
import static com.casic.worklog.model.vo.Result.ok;

@RestController
/**
 * @author hz
 * */
public class EmployController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 查询人员信息
     *
     * @param pageNum  当前页
     * @param pageSize 每页显示的条数
     * @return 附带分页信息的员工信息
     */
    @GetMapping("/findUserBy")
    public Result findUserBy (@RequestParam(defaultValue = "1") int pageNum, @RequestParam
            (defaultValue = "10") int pageSize, String name, String startDate, String endDate, Long phone, Integer position) {
        //前台数据传入对象
        Employee employee = new Employee();
        employee.setName(name);
        employee.setPhone(phone);
        employee.setPosition(position);
        //转换时间格式存入对象
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if (startDate != null && startDate != "") {
                employee.setStartDate(format.parse(startDate));
            }
            if (endDate != null && endDate != "") {
                employee.setEndDate(format.parse(endDate));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Result result = employeeService.findUserBy(pageNum, pageSize, employee);
        return result;
    }

    /**
     * 员工信息导出接口
     * */

    /**
     * 导出
     * @param response
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) {
        try {
            List<Employee> list = employeeService.pageComment();
            ExcelUtils.exportExcel(list, "员工信息表", "员工信息表", Employee.class, "员工信息表", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 员工批量添加
     * @param file
     * @return
     */
    @PostMapping("/saveByExcel")
    public BsResult saveByExcel(@RequestParam("file") MultipartFile file) {
        String warning ="";
        int successNum = 0;
        try {
            List<Employee> list = ExcelUtils.importExcel(file, 1, 1, Employee.class);
            if (list != null) {
                for (Employee member : list) {
                    if (member.getName()!=null){
                        if(employeeService.findByUsername(member.getUsername())!=null){
                            warning = warning +member.getName()+"账号名已存在,";
                            continue;
                        }else {
                            employeeService.insertUser(member);
                            successNum++;
                        }
                    }
                }
            }
            return BsResult.ok("插入成功"+successNum+"条").put("warning ",warning);
        } catch (Exception e) {
            e.printStackTrace();
            return BsResult.error(500, e.getMessage());
        }
    }


    /**
     * 删除员工信息
     */
    @RequestMapping("/deleteEmploy")
    public Object deleteEmploy (String id) {

        if (!CommonUtil.isEmpty(id)) {
            int i;
            if (id.contains(",")) {
                int[] sid = CommonUtil.strArrayToIntArray(id.split(","));
                //批量删除
                 i = (int) employeeService.deleteArrays(sid);
            } else {
                //单条
                Short sid = Short.valueOf(id);
                i = employeeService.deleteEmploy(sid);
            }
            if (i > 0) {
                return ok("删除成功");
            } else {
                return error("删除失败");
            }
        }
        return error("删除失败，未传入唯一标识");
    }

    /**
     * 添加或更新人员信息
     */
    @RequestMapping("/save")
    public Result save (Employee employee) {

        if (employee.getId() == null) {
            //入职时间
            employee.setHiredate(new Date());

            if(employee.getType()==null){
            //默认类型为普通员工
            employee.setType(Employee.ORDINARY_STAFF);
            }
            employeeService.insertUser(employee);
            return Result.ok();
        } else {
            employeeService.updateUser(employee);
            return Result.ok();
        }

    }

}
